package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping
    public Result  list(UserDto userDto) {
        log.info("查询用户列表{}", userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    @Log
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("新增用户{}" , user);
        userService.addUser(user);
        return Result.success();
    }
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除用户{}" , ids);
        userService.deleteUserById(ids);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result getById(@PathVariable  Integer id){
        log.info("查询用户{}", id);
        User userList = userService.selectUserById(id);
        return Result.success(userList);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody User user){
        log.info("更新用户{}" , user);
        userService.updateUser(user);
        return Result.success();
    }
    //查询所有用户
    @GetMapping("/list")
    public Result selectAllUser(@PathVariable List<User> userAllUsersList) {
        log.info("查询所有用户");
        List<User> userList = userService.selectAllUser();
        return Result.success(userList);
    }
    @GetMapping("/role/{roleLabel}")
    public Result selectUserByRole(@PathVariable String roleLabel) {
        log.info("查询用户{}", roleLabel);
        List<User> userList = userService.selectUserByRole(roleLabel);
        return Result.success(userList);
    }
}
